﻿package com.scn.base.util;

import java.util.Random;


public class ShortUrl {
	private static long radix = 62;

	//将62进制的整数转换为十进制的整数
	public static long destToTen(String num) {
		long result = 0;
		num=reverseWords(num);
		for (int i = 0; i < num.length(); i++) {
			char c = num.charAt(i);
			if (c < '0' || (c > '9' && c < 'A') || (c > 'Z' && c < 'a')
					|| c > 'z') {
				return -1;
			}
			long digit;
			if (c < 'A')
				digit = c - '0';
			else if (c >= 'a')
				digit = c - 'a' + 36;
			else
				digit = c - 'A' + 10;
			for (int j = i + 1; j < num.length(); j++) {
				digit = digit * radix;
			}
			result = result + digit;
		}
		return result;
	}
	/**
	 * 字母大小写反转
	 * 
	 * @param src
	 * @return
	 */
	private static String reverseWords(String src){
		StringBuffer result = new StringBuffer();
		for (int i = 0; i < src.length(); i++) {
			char c = src.charAt(i);
			if(c>='a'&&c<='z'){
				result.append((char)(c-32));
			}else if(c>='A'&&c<='Z'){
				result.append((char)(c+32));
			}else{
				result.append(c);
			}
		}
		return result.toString();
	}
	//将十进制的整数转换为62进制的整数
	public static String tenToDest(long num) {
		StringBuffer result = new StringBuffer();
		while (!(num < radix)) {
			long mod = num % radix;
			if (mod < 10)
				result.append((char) ('0' + mod));
			else if (mod >= 36)
				result.append((char) ('a' + mod - 36));
			else
				result.append((char) ('A' + mod - 10));
			num = num / radix;
		}
		if (num < 10)
			result.append((char) ('0' + num));
		else if (num >= 36)
			result.append((char) ('a' + num - 36));
		else
			result.append((char) ('A' + num - 10));
		result = result.reverse();
		return reverseWords(result.toString());
	}
	
	//将指定进制的整数转换为十进制的整数(备用)
	private int currentRadixToDecimal(String num, int radix) {
		int result = 0;
		if (radix > 36 || radix < 2) {
			System.out.println("The radix should be between 2 and 36.");
			System.exit(1);
		}
		num = num.toLowerCase();
		char upperLimitChar;
		if (radix < 11) {
			upperLimitChar = (char) ('1' + radix - 2);
			for (int i = 0; i < num.length(); i++) {
				char c = num.charAt(i);
				if (c < '0' || c > upperLimitChar) {
					System.out.println("Invalid character: " + c);
					System.exit(1);
				}
				int digit = c - '0';
				for (int j = i + 1; j < num.length(); j++) {
					digit = digit * radix;
				}
				result = result + digit;
			}
			return result;
		} else {
			upperLimitChar = (char) ('a' + radix - 11);
			for (int i = 0; i < num.length(); i++) {
				char c = num.charAt(i);
				if (c < '0' || (c > '9' && c < 'a') || c > upperLimitChar) {
					System.out.println("Invalid character: " + c);
					System.exit(1);
				}
				int digit;
				if (c < 'a')
					digit = c - '0';
				else
					digit = c - 'a' + 10;
				for (int j = i + 1; j < num.length(); j++) {
					digit = digit * radix;
				}
				result = result + digit;
			}
			return result;
		}
	}

	//将十进制的整数转换为指定进制的整数(备用)
	private String decimalToTargetRadix(int num, int radix) {
		StringBuffer result = new StringBuffer();
		if (radix > 36 || radix < 2) {
			System.out.println("The radix should be between 2 and 36.");
			System.exit(1);
		}
		while (!(num < radix)) {
			int mod = num % radix;
			if (mod < 10)
				result.append((char) ('0' + mod));
			else
				result.append((char) ('a' + mod - 10));
			num = num / radix;
		}
		result.append(num < 10 ? ((char) ('0' + num))
				: ((char) ('a' + num - 10)));
		result = result.reverse();
		return result.toString();
	}
	private static String[] pri={"00000","0000","000","00","0",""};
	public static long generalId(){
		long curr=System.currentTimeMillis();
		Random ran=new Random();
		int intTemp=ran.nextInt(100000);
		String temp=pri[String.valueOf(intTemp).length()]+curr+intTemp;
		return Long.parseLong(temp);
	}
	
	public static void main(String[] args) {
		long a=generalId();
		System.out.println(ShortUrl.tenToDest(a));
		System.out.println(ShortUrl.destToTen("9CRyApJFNz"));
		System.out.println(a);
		//130319313071812685
	}
}
